<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Micro Router class
 * 
 * @package    Core
 * @author     Andres Vidal
 * @copyright  (c) 2009 Andres Vidal
 * @license    http://www.andresvidal.com/license/micro
 */
 
class Router {

	protected static $routes;
	
	public static $query_string = '';
	public static $current_uri  = '';
	public static $complete_uri = '';
	
	/**
	 * Router setup routine. Automatically called after Micro setup process.
	 *
	 * @todo            add exception handling
	 * @return  void
	 */
	public static function setup()
	{
		if ( ! empty($_SERVER['QUERY_STRING']))
		{
			// Set the query string to the current query string
			Router::$query_string = '?'.trim($_SERVER['QUERY_STRING'], '&/');
		}
		
		if (Router::$routes === NULL)
		{
			// Load routes
			Router::$routes = Micro::config('routes');
		}
		
		// Default route status
		//$default_route = FALSE;

		if (Router::$current_uri === '')
		{
			// Make sure the default route is set
			if ( ! isset(Router::$routes['_default']))
				//throw new Micro_Exception('no_default_route'); OR set to index.php
				die('no default route set');

			// Use the default route when no segments exist
			Router::$current_uri = Router::$routes['_default'];

			// Default route is in use
			//$default_route = TRUE;
		}
		
		// Make sure the URL is not tainted with HTML characters
		Router::$current_uri = htmlspecialchars(Router::$current_uri, ENT_QUOTES, 'UTF-8', FALSE);
		
		// Remove all dot-paths from the URI, they are not valid
		Router::$current_uri = preg_replace('#\.[\s./]*/#', '', Router::$current_uri);
		
		// At this point segments, rsegments, and current URI are all the same
		//Router::$segments = Router::$rsegments = Router::$current_uri = trim(Router::$current_uri, '/');
		
		// Set the complete URI
		Router::$complete_uri = Router::$current_uri.Router::$query_string;
		
		// Explode the segments by slashes
		//Router::$segments = ($default_route === TRUE OR Router::$segments === '') ? array() : explode('/', Router::$segments);
		
		if ( ! is_file( PAGES . Router::$current_uri ) ) {
			
			// Load error pages
			$error_pages = Micro::config('error_pages');
			$error_pages = ( array_key_exists('404', $error_pages) ) ? SYSPATH . $error_pages['404'] : '';
			
			header("HTTP/1.0 404 Not Found");			
			include($error_pages);
		
		} else {

			// Load the requested page into $output
			Micro::$output = PAGES . Router::$current_uri;
		}
	}
	
	/**
	 * Attempts to determine the current URI using PATH_INFO, ORIG_PATH_INFO, or PHP_SELF.
	 *
	 * @return  void
	 */
	public static function find_uri()
	{
		if (isset($_SERVER['PATH_INFO']) AND $_SERVER['PATH_INFO'])
		{
			Router::$current_uri = $_SERVER['PATH_INFO'];
		}
		elseif (isset($_SERVER['ORIG_PATH_INFO']) AND $_SERVER['ORIG_PATH_INFO'])
		{
			Router::$current_uri = $_SERVER['ORIG_PATH_INFO'];
		}
		elseif (isset($_SERVER['PHP_SELF']) AND $_SERVER['PHP_SELF'])
		{
			Router::$current_uri = $_SERVER['PHP_SELF'];
		}

		// The front controller directory and filename
		$fc = substr(realpath($_SERVER['SCRIPT_FILENAME']), strlen(DOCROOT));

		if (($strpos_fc = strpos(Router::$current_uri, $fc)) !== FALSE)
		{
			// Remove the front controller from the current uri
			Router::$current_uri = substr(Router::$current_uri, $strpos_fc + strlen($fc));
		}

		// Remove slashes from the start and end of the URI
		Router::$current_uri = trim(Router::$current_uri, '/');
	}
}